<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>标签信息管理</span>
      </div>
    </template>
    <el-row class="container" v-for="(item,index) in labelTypeList" :key="index">
      <el-col :span="2" class="text-center">{{ item.label }}标签</el-col>
      <el-col :span="22" class="item-container">
        <el-popconfirm
          v-for="item2 in item.children || []"
          :key="item2.id"
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteLabelHandle(item2)"
          @cancel="editLabelHandle(item2)"
          :title="`修改或删除${item2.name}`"
        >
          <template #reference>
            <div class="item m-r-5 pointer">
              <el-tag style="padding: 13px">{{ item2.name }}</el-tag>
            </div>
          </template>
        </el-popconfirm>
        <el-icon class="item m-r-5 pointer" :size="35" color="#409EFF" @click="addLabelHandle(item)">
          <CirclePlus />
        </el-icon>
      </el-col>
    </el-row>
    <!--标签信息修改弹窗管理-->
    <el-dialog v-model="dialog.visible" :destroy-on-close="true" :title="dialog.title" style="max-width: 700px">
      <el-form ref="labelFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :step="10" class="!w-100%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import { LabelVO, LabelForm, LabelQuery } from '@/api/apartment/attribute/label/types';
import { listLabel, getLabel, addLabel, updateLabel, delLabel } from '@/api/apartment/attribute/label/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const buttonLoading = ref(false);
const labelFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFormData: LabelForm = {
  id: undefined,
  type: undefined,
  name: undefined,
  sort: undefined
};
const data = reactive<PageData<LabelForm, LabelQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: undefined,
    name: undefined,
    sort: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '标签名称不能为空', trigger: 'blur' }],
    sort: [{ required: true, message: '排序号不能为空', trigger: 'blur' }]
  }
});
const { queryParams, form, rules } = toRefs(data);
type SupportLabelInterface = {
  label: string;
  value: string;
  children?: LabelVO[];
};
const BuildingTypeTypeMap = [
  { label: '公寓', value: 1 },
  { label: '房间', value: 2 }
];
// 标签类型
const labelTypeList = ref<SupportLabelInterface[]>([]);
// 获取标签信息
const getLabelInfoListHandle = async () => {
  const { data } = await listLabel(queryParams.value);
  labelTypeList.value = JSON.parse(JSON.stringify(BuildingTypeTypeMap));
  data.forEach((item) => {
    // 获取对应类型的名称
    const targetType = labelTypeList.value.find((item2) => item2.value === item.type);
    if (targetType) {
      targetType.children = targetType.children || [];
      targetType.children.push(item);
    }
  });
};
// 删除标签
const deleteLabelHandle = async (item: LabelVO) => {
  const _ids = item?.id;
  await proxy?.$modal.confirm('是否确认删除标签信息编号为"' + _ids + '"的数据项？');
  await delLabel(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getLabelInfoListHandle();
};
// 编辑标签
const editLabelHandle = async (item: LabelVO) => {
  reset();
  const _id = item?.id;
  const res = await getLabel(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改标签信息';
};
// 添加标签
const addLabelHandle = (item: SupportLabelInterface) => {
  reset();
  form.value.type = item.value;
  dialog.visible = true;
  dialog.title = '添加' + item.label + '标签信息';
};
/** 提交按钮 */
const submitForm = () => {
  labelFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateLabel(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addLabel(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getLabelInfoListHandle();
    }
  });
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  labelFormRef.value?.resetFields();
};
onMounted(() => {
  getLabelInfoListHandle();
});
</script>

<style scoped lang="scss">
.card-header {
  font-size: 18px;
  font-weight: bold;
}

.text-center {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
}

.container:not(:first-child) {
  margin-top: 20px;
}

.item-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 10px 15px;
  background-color: #efefef;
  border-radius: 20px;

  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
